package com.atstudying.mall.product.controller;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.R;
import com.atstudying.mall.product.entity.AttrAttrgroupRelationEntity;
import com.atstudying.mall.product.entity.AttrEntity;
import com.atstudying.mall.product.entity.AttrGroupEntity;
import com.atstudying.mall.product.service.AttrAttrgroupRelationService;
import com.atstudying.mall.product.service.AttrGroupService;
import com.atstudying.mall.product.service.AttrService;
import com.atstudying.mall.product.service.CategoryService;
import com.atstudying.mall.product.vo.AttrGroupRelationVo;
import com.atstudying.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author zlj
 * @email 2908434305@qq.com
 * @date 2020-10-04 22:40:50
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    AttrService attrService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){

        //1：查出当前分类下的所有属性分组

        //2：查出每个属性分组的所有属性

        List<AttrGroupWithAttrsVo> vos= attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);

    }

///product/attrgroup/attr/relation
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){

    relationService.saveBatch(vos);
    return R.ok();
}


@PostMapping("/attr/relation/delete")
//PostMapping请求写带来json数据，我们自定义的对象必须加一个注解（将请求体的json数据封装给vos
    ///product/attrgroup/attr/relation/delete
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] vos ){

        attrService.deleteRelation(vos);

        return R.ok();

    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId ){

        List<AttrEntity> entities=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,@RequestParam Map<String,Object> param) {

        PageUtils page =attrService.getNoRelationAttr(attrgroupId,param);
        return R.ok().put("page",page);

    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //路径变量的id，我们要使用@PathVariable来标明它是一个路径
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		Long catelogId=attrGroup.getCatelogId();
		//通过catelogid来查找出它的完整路径id
		Long[] catelogPath= categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
